<?php
    class Friendlink extends MY_Controller {


        //友情链接列表页初始化
        public function index()
        {

            $ischeck = $this->input->get('ischeck',true); 
            if(empty($ischeck))
            {
                $ischeck=0;
            }
            $keyword = $this->input->get('keyword',true);


            $whereSql =" WHERE 1=1 ";

            if($keyword != '')
            {
                $whereSql .= " AND CONCAT(`url`,`webname`,`email`) LIKE '%$keyword%' ";
            }
            if($ischeck!=0)
            {
                $whereSql .= " And ischeck='$ischeck'  "; 
            }

            $query="SELECT * FROM `crazy_flink` $whereSql ";


            $list=$this->db->query($query);

            //从数据库中获取列表总数
            $num= $list->num_rows();

            //查询条件
            $search_str="keyword=$keyword&ischeck=$ischeck";

            //分类数据初始化
            $params = array('total' => $num, 'listRows' => '15','pa'=>$search_str);

            //分页类初始化
            $this->load->library('Page',$params);

            //分页SQL语句
            $search_query= $query."  limit {$this->page->limit} ";

            //获取分页结果
            $reslut_list= $this->db->query($search_query);

            $data['list']=$reslut_list;

            $data['page']= $this->page->fpage();

            $selCheckArr = array(0=>'不限类型', 3=>'未审核', 1=>'内页', 2=>'首页');
            $data['selCheckArr']=$selCheckArr;
            $data['ischeck']=$ischeck;
            $this->load->view('admin/friendlink_index',$data);
        }

        //添加页面初始化
        public function add()
        {
            $this->load->model('admin/Friendlink_model');
            $data['art_list']=$this->Friendlink_model->flinktype_all_list();
            $this->load->view('admin/friendlink_add',$data); 
        }
        public function save()
        {
            $this->load->helper('common_function');
            $dopost = $this->input->post('dopost',true); 
            $this->load->model('admin/Friendlink_model');
            if($dopost=="add")
            {
                //网址
                $url = $this->input->post('url',true); 
                //网站名称
                $webname = $this->input->post('webname',true); 
                //排列位置
                $sortrank = $this->input->post('sortrank',true);
                //网站Logo 
                $logo = $this->input->post('logo',true); 
                //网站简况
                $msg = $this->input->post('msg',true); 
                //站长Email
                $email = $this->input->post('email',true);
                //网站类型
                $typeid = $this->input->post('typeid',true);
                //链接位置：
                $ischeck = $this->input->post('ischeck',true);


                $data = array(
                    'sortrank' => $sortrank ,
                    'url' => $url,
                    'webname' => $webname,
                    'msg' => $msg,
                    'email' => $email,
                    'logo' => $logo,
                    'dtime' =>  time(),
                    'typeid' => $typeid,
                    'ischeck' => $ischeck
                );
                $this->Friendlink_model->friendlink_save_add($data);
                ShowMsg('添加成功!', site_url("admin/friendlink"),0,1000);
                exit; 
            }
            if($dopost=="modify")
            {
                $id = $this->input->post('id',true); 
                //网址
                $url = $this->input->post('url',true); 
                //网站名称
                $webname = $this->input->post('webname',true); 
                //排列位置
                $sortrank = $this->input->post('sortrank',true);
                //网站Logo 
                $logo = $this->input->post('logo',true); 
                //网站简况
                $msg = $this->input->post('msg',true); 
                //站长Email
                $email = $this->input->post('email',true);
                //网站类型
                $typeid = $this->input->post('typeid',true);
                //链接位置：
                $ischeck = $this->input->post('ischeck',true);


                $data = array(
                    'sortrank' => $sortrank ,
                    'url' => $url,
                    'webname' => $webname,
                    'msg' => $msg,
                    'email' => $email,
                    'logo' => $logo,
                    'typeid' => $typeid,
                    'ischeck' => $ischeck
                );
                $this->Friendlink_model->friendlink_save_modify($id,$data);
                ShowMsg('编辑成功!', site_url("admin/friendlink"),0,1000);
                exit; 
            }
        }

        //编辑页面初始化
        public function modify()
        {
            $id = $this->input->get('id',true); 
            $this->load->model('admin/Friendlink_model');
            $data['model']=$this->Friendlink_model->get_friendlink_by_id_one($id);
            $data['art_list']=$this->Friendlink_model->flinktype_all_list();
            $this->load->view('admin/friendlink_modify',$data); 
        }

        //删除
        public function del()
        {
          
            $this->load->helper('common_function');
            $aids = $this->input->get('aids',true); 
            $aids = explode(',',$aids);
            if(!empty($aids) && is_array($aids))
            {
                foreach($aids as $aid)
                {
                    $aid = preg_replace("#[^0-9]#", "", $aid);
                    $this->load->model('admin/Friendlink_model');
                    $this->Friendlink_model->get_friendlink_del($aid);
                }
                ShowMsg('删除成功!', site_url("admin/friendlink"),0,1000);
               exit; 
            }
            else
            {
                ShowMsg('删除失败!', site_url("admin/friendlink"),0,1000);
                exit; 
            }
        }
    }
?>